<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/9/23
 * Time: 14:26
 */

namespace app\portal\controller;
use cmf\controller\AdminBaseController;
use app\portal\model\DocumentCategoryModel;
use app\portal\model\DocumentModel;

class AdminDocumentController extends AdminBaseController
{
    //文档列表
    public function index(){
        $where=[];
        $assign=[];
        $param = $this->request->param();
        if(!empty($param['keyword'])){
            $where[]=['title','like','%'.$param['keyword'].'%'];
            $assign['keyword']=$param['keyword'];
        }
        $categoryId = $this->request->param('category');
        if(!empty($categoryId)){
            $where[]=['category','=',$categoryId];
            $assign['cate_id']=$categoryId;
        }else{
            $assign['cate_id']=0;
        }
       //dump($where);
        $docCategoryModel = new DocumentCategoryModel();
        $docModel= new DocumentModel();
        $category=$docCategoryModel->select();
        $assign['category']=$category;
        $data=$docModel->with('categories')->with('user')->where($where)->paginate(10);
        foreach ($data as $k=>$v){
            $v['file_urls']=json_decode($v['file_urls']);
            $v['file_names']=json_decode($v['file_names']);
            $v['post']=json_decode($v['post'],true);
        }
        //dump($data);
        $assign['data']=$data;
        $assign['page']=$data->render();
        $this->assign($assign);
        return $this->fetch();
    }
    //添加文档页面
    public function document_add(){
        $docCategoryModel = new DocumentCategoryModel();
        $category=$docCategoryModel->select();
        $this->assign(['category'=>$category]);

        return $this->fetch();
    }
    //保存添加文档数据
    public function save_add_document(){
        $docModel = new DocumentModel();
        $data=$this->request->param();
        $result = $this->validate($data, 'AdminDocument');
        if ($result !== true) {
            $this->error($result);
        }
        $data['file_urls']=json_encode($data['file_urls']);
        $data['post']=json_encode($data['post']);
        $data['file_names']=json_encode($data['file_names']);
        $data['ctime']=time();
        $data['admin']=session('ADMIN_ID');
        $result = $docModel->insert($data);
        if ($result === false) {
            $this->error('添加失败!');
        }
        $this->success('添加成功!', url('AdminDocument/index'));
    }
    //修改文档页面
    public function document_edit(){
        $id = $this->request->param('id', 0, 'intval');
        $docCategoryModel = new DocumentCategoryModel();
        $category=$docCategoryModel->select();
        if ($id > 0) {
            $DocumentModel = new DocumentModel();
            $document           = $DocumentModel->find($id);
            $this->assign(['category'=> $category,'data'=>$document]);
            return $this->fetch();
        } else {
            $this->error('操作错误!');
        }
        return $this->fetch();
    }
    //保存修改文档数据
    public function save_edit_document(){
        $docModel = new DocumentModel();
        $data=$this->request->param();
        $result = $this->validate($data, 'AdminDocument');
        if ($result !== true) {
            $this->error($result);
        }
        $data['file_urls']=json_encode($data['file_urls']);
        $data['post']=json_encode($data['post']);
        $data['file_names']=json_encode($data['file_names']);
        $data['ctime']=time();
        $data['admin']=session('ADMIN_ID');
        $result = $docModel->update($data);
        if ($result === false) {
            $this->error('保存失败!');
        }
        $this->success('保存成功!', url('AdminDocument/index'));
    }
    //删除文档
    public function document_del(){
        $Model = new DocumentModel();
        $id= $this->request->param('id');
        $result=$Model->where('id',$id)->delete();
        if ($result) {
            $this->success('删除成功!');
        } else {
            $this->error('删除失败');
        }
    }
    //文档分类列表
    public function category(){
        $docCategoryModel = new DocumentCategoryModel();
        $categories=$docCategoryModel->select();
        $this->assign('categories',$categories);
        return $this->fetch();
    }
    //添加分类页面
    public function category_add(){
        return $this->fetch();
    }
    //修改分类页面
    public function category_edit(){
        $id = $this->request->param('id', 0, 'intval');
        if ($id > 0) {
            $CategoryModel = new DocumentCategoryModel();
            $category            = $CategoryModel->find($id);
            $this->assign('category', $category);
            return $this->fetch();
        } else {
            $this->error('操作错误!');
        }
    }
    //保存添加分类页面
    public function save_add_category(){
        $docCategoryModel = new DocumentCategoryModel();
        $data = $this->request->param();
        $result = $this->validate($data, 'AdminDocumentCategory');
        if ($result !== true) {
            $this->error($result);
        }
        $result = $docCategoryModel->insert($data);
        if ($result === false) {
            $this->error('添加失败!');
        }
        $this->success('添加成功!', url('AdminDocument/category'));
    }
    //保存修改分类页面
    public function save_edit_category(){
        $docCategoryModel = new DocumentCategoryModel();
        $data = $this->request->param();
        $result = $this->validate($data, 'AdminDocumentCategory');
        if ($result !== true) {
            $this->error($result);
        }
        $result = $docCategoryModel->update($data);
        if ($result === false) {
            $this->error('修改失败!');
        }
        $this->success('修改成功!', url('AdminDocument/category'));
    }
    //删除分类
    public function cate_del(){
        $docCategoryModel = new DocumentCategoryModel();
        $id= $this->request->param('cate_id');
        $result=$docCategoryModel->where('id',$id)->delete();
        if ($result) {
            $this->success('删除成功!');
        } else {
            $this->error('删除失败');
        }
    }
}